﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTravian
{
    public partial class traRobForm : Form
    {
        protected HtmlElementCollection troops = null;
        protected HtmlDocument Doc = null;
        /// <summary>
        /// 取得目前要出去的那個項目
        /// </summary>
        protected HtmlElement currentOutTroop = null;

        public traRobForm()
        {
            InitializeComponent();
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Doc = webBrowser1.Document;
            HtmlElement td = Doc.GetElementById("troops");
            troops = td.GetElementsByTagName("td");         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text.Equals("Hijacker"))
            {
                button1.Text = "STOP";
                timer1.Start();
            }
            else
            { 
                button1.Text = "Hijacker" ;
                timer1.Stop();
            }
        }


        private void SetTroops()
        {
            browserIsReadey();
            foreach (HtmlElement troop in troops)
            {
                HtmlElementCollection soldier_img = troop.GetElementsByTagName("img");
                HtmlElementCollection soldier_text = troop.GetElementsByTagName("input");
                if (soldier_img.Count > 0 & soldier_text.Count > 0)
                {
                    cmb_soldier.Items.Add(soldier_img[0].GetAttribute("title").ToString());
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetTroops();
        }

        protected void browserIsReadey()
        {
            webBrowser1.Navigate(txt_url.Text);

            while (webBrowser1.IsBusy || string.IsNullOrEmpty(webBrowser1.StatusText))
            {
                System.Threading.Thread.Sleep(1000);
                Application.DoEvents();
            }           
        }

        /// <summary>
        /// 檢查是否已有選擇了
        /// </summary>
        /// <returns>沒有選回傳false</returns>
        bool validateIsSelectSoldier()
        {
            string soldier = cmb_soldier.SelectedItem.ToString();
            return string.IsNullOrEmpty(soldier) ? false : true;
        }

        /// <summary>
        /// 檢查是否已有輸入了出發人數
        /// </summary>
        /// <returns></returns>
        bool validateIsSoldierCount()
        {
            return string.IsNullOrEmpty(txt_soldierCount.Text) ? false : true;
        }

        /// <summary>
        /// 找出目前要出門去的那個士兵項目
        /// </summary>
        void GetSelectTroops()
        {
            currentOutTroop = null;
            string soldier = cmb_soldier.SelectedItem.ToString();
            //依所選的人進行檢查
            foreach (HtmlElement troop in troops)
            {
                HtmlElementCollection soldier_img = troop.GetElementsByTagName("img");
                HtmlElementCollection soldier_text = troop.GetElementsByTagName("input");
                if (soldier_img.Count > 0 & soldier_text.Count > 0)
                {
                    if (soldier == soldier_img[0].GetAttribute("title").ToString())
                    {
                        HtmlElementCollection soldier_a = troop.GetElementsByTagName("a");

                        if (soldier_a.Count > 0) //有可能選到一個沒有人的項目..所以 要檢查
                        {
                            string currentSoldier = soldier_a[0].InnerText.Replace("(", "").Replace(")", "");
                            if (!string.IsNullOrEmpty(currentSoldier))
                            {
                                if (Convert.ToInt32(currentSoldier) >= Convert.ToInt32(txt_soldierCount.Text))
                                {
                                    currentOutTroop = soldier_text[0];
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            browserIsReadey();
            //依所選的人來確認目前是否有足夠的人可以出門


            timer1.Start();
        }


        void Hijacker()
        {
            try
            {
                HtmlElement  x, y, c;

                System.Collections.ArrayList xy = GetXYElementCollection();
                x = GetXYElement(xy, "x", "name");
                y = GetXYElement(xy, "y", "name");
                c = GetXYElement(xy, "4", "value");
                x.SetAttribute("value", "133");
                y.SetAttribute("value", "18");
                currentOutTroop.SetAttribute("value", txt_soldierCount.Text);
                //c.InvokeMember("click");  //.SetAttribute("checked", "true");                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            //btn_ok = doc.GetElementById("btn_ok");
            //btn_ok.InvokeMember("click");
               
        }

        HtmlElement GetXYElement(System.Collections.ArrayList xys, string strXY, string strAttribute)
        {
            HtmlElement xy = null;
            foreach (HtmlElement obj in xys)
            {
                if (obj.GetAttribute(strAttribute).ToLower().Equals(strXY))
                {
                    xy = obj;
                    break;
                }
            }
            return xy;
        }


        System.Collections.ArrayList GetXYElementCollection()
        {
            System.Collections.ArrayList xy = new System.Collections.ArrayList();
            HtmlDocument doc = webBrowser1.Document;
            foreach (HtmlElement input in doc.GetElementsByTagName("input"))
            {
                if (input.GetAttribute("name").ToLower().Equals("x") 
                    || input.GetAttribute("name").ToLower().Equals("y")
                    || input.GetAttribute("name").ToLower().Equals("c")
                    )
                {
                    xy.Add(input);
                }
            }

            return xy;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            


            //if (!validateIsSelectSoldier())
            //{ return; }
            //if (!validateIsSoldierCount())
            //{ return; }
            //  GetSelectTroops();
            //if (currentOutTroop == null)
            //{ return; }
            //Hijacker();
        }

        private void traWebControl1_Load(object sender, EventArgs e)
        {

        }
    }
}
